const basicType = {
  color: {
    method: 'addColor',
    getValue: item => item.color.getStyle(),
    setValue: (item, value) => item.color.setStyle(value),
  },
  intensity: {
    extends: [0, 10],
    getValue: item => item.intensity,
    setValue: (item, value) => item.intensity = +value,
  },
  distance: {
    extends: [0, 2],
    getValue: item => item.distance,
    setValue: (item, value) => item.distance = +value,
  },
  angle: {
    extends: [0, Math.PI / 2],
    getValue: item => item.angle,
    setValue: (item, value) => item.angle = +value,
  },
  exponent: {
    extends: [0, 20],
    getValue: item => item.exponent,
    setValue: (item, value) => item.exponent = +value,
  },
  decay: {
    extends: [0, 5],
    getValue: item => item.decay,
    setValue: (item, value) => item.decay = +value,
  },
  penumbra: {
    extends: [0, 1],
    getValue: item => item.penumbra,
    setValue: (item, value) => item.penumbra = +value,
  },
  groundColor: {
    method: 'addColor',
    getValue: item => item.groundColor.getStyle(),
    setValue: (item, value) => item.groundColor.setStyle(value),
  },
}

const itemType = {
  // 聚光灯
  SpotLight: ['color', 'intensity', 'distance', 'angle', 'exponent', 'decay', 'penumbra'],
  // 环境光
  AmbientLight: ['color'],
  // 点光源
  PointLight: ['color', 'intensity', 'distance'],
  // 平行光
  DirectionalLight: ['color', 'intensity'],
  // 半球光
  HemisphereLight: ['color', 'groundColor', 'intensity']
}

function initControls(item) {
  const typeList = itemType[item.type];
  const controls = {};
  if (!typeList || !typeList.length) {
    return;
  }
  const gui = new dat.GUI();

  for (let i = 0; i < typeList.length; i++) {
    const child = basicType[typeList[i]];

    // 兼容各个版本的差异，如果示例不存在该属性则不显示监听
    if (child && item[typeList[i]] !== undefined) {
      controls[typeList[i]] = child.getValue(item);

      const childExtends = child.extends || [];

      gui[child.method || 'add'](controls, typeList[i], ...childExtends).onChange((value) => {
        child.setValue(item, value);
      });
    }
  }
}
